package com.xiaohao.ctdemo.service;

import com.mathworks.toolbox.javabuilder.MWException;
import com.xiaohao.ctdemo.bean.Image;
import com.xiaohao.similarity.Similarity;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author 小浩
 * @date 2018-07-22 09:52
 * @package com.xiaohao.ctdemo.service
 * @description
 */

@Service
public class ImageService {
    private Similarity similarity = new Similarity();

    public ImageService() throws MWException {
    }

    @Cacheable(value = "ImageInfo")
    public List<Image> getImageInfo(int id) throws MWException, UnsupportedEncodingException {

        System.out.println("no cache");
        Object[] objects = similarity.similarity(2, id, "./data.mat");
        List<Image> list = new LinkedList<>();
        String[] ids = objects[0].toString().split(" ");
        List<String> idss = new ArrayList();
        String[] label = objects[1].toString().split("\n");
        for (String str : ids) {
            if (!str.equals("")) {
                idss.add(str);
            }
        }
        for (int i = 0; i < label.length; i++) {
            Image image = new Image();
            image.setId(Integer.parseInt(idss.get(i)));
            image.setLabel(new Integer(label[i].replace(" ", "")));
            list.add(image);
        }
        return list;
    }

}
